// haojk/pages/grade/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */

    data: {
        credit1: "0.00",
        type:0,
        currentTab:0 ,
        showView: false,
        //userPrivilege: "盟主特权",
        //发送请求的用户种类
        sendUserType:1,
        //打钩 
        showCorrectImg:1,
        triangleStyle:false,
        //用户等级显示
        //userGrade:2,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var memberlistinfoObj=  that.data.memberlistinfo;

        var globalset = wx.getStorageSync('globalset');
        var buttonText = globalset.memberlevel[0].identityname;
        that.setData({ buttonText: buttonText});
        
        that.setData({
            userPrivilege: buttonText+"特权",
        })

    

        that.getLevel();
      

    },
    //发送请求捞数据
    getLevel:function(){
        var $that = this;
        app.util.request({
            url: 'entry/wxapp/Level',
            data: {
                "m": 'nets_haojk',
            },
            method: 'post',
            success: function (response) {
                console.log(response);
             
                var obj = response.data.data;
                var vipArray = [];
               
                var item ='';
                //捞出数据处理
                var newMemberlistinfo = [];
                if (obj != "" && obj != undefined && obj != null){
                    //给上面的等级赋值
                    for(var i=0;i<obj.length;i++){
                        if(obj[i].type == 1){
                            newMemberlistinfo.push(obj[i]);
                        }
                    }
                    //数组排序

                    function compare(property) {
                        return function (a, b) {
                            var value1 = a[property];
                            var value2 = b[property];
                            return value1 - value2;
                        }
                    }
                    newMemberlistinfo.sort(compare('name'));
                  
                    //吧type 为1 的排序后的数组放入混存
                    wx.setStorage(
                        {
                            key: "newMemberlistinfo",
                            data: newMemberlistinfo
                        }
                    )

                    for (var i = 0; i < newMemberlistinfo.length;i++){
                        if (newMemberlistinfo[i].name==0){
                            newMemberlistinfo[i].membername='普通盟主'
                        } else if(newMemberlistinfo[i].name == 1){
                            newMemberlistinfo[i].membername = '白银盟主'
                        } else if (newMemberlistinfo[i].name == 2) {
                            newMemberlistinfo[i].membername = '黄金盟主'
                        } else if (newMemberlistinfo[i].name == 3) {
                            newMemberlistinfo[i].membername = '铂金盟主'
                        } else if (newMemberlistinfo[i].name == 4) {
                            newMemberlistinfo[i].membername = '钻石盟主'
                        }
                    }
                   
                    $that.setData({
                        newMemberlistinfo: newMemberlistinfo
                    })
                    wx.setStorage({
                        key: "memberlistinfo",
                        data: newMemberlistinfo
                    });
                  
                    //下面九宫格选择展示
                    if ($that.data.sendUserType==1){
                        for (var i = 0; i < obj.length; i++) {
                            if (obj[i].type == 1) {
                                vipArray.push(obj[i]); 
                            }
                        }
                        //数组排序

                        function compare(property) {
                            return function (a, b) {
                                var value1 = a[property];
                                var value2 = b[property];
                                return value1 - value2;
                            }
                        }
                        vipArray.sort(compare('name'));
                        $that.setData({levelTable: vipArray});
                        console.log($that.data.levelTable);
                    } else if ($that.data.sendUserType == 0){
                        for (var i = 0; i < obj.length; i++) {
                            if (obj[i].type == 0) {
                                item = obj[i];
                            }
                        }
                        //复制填充用户的信息
                        for (var j = 0; j < newMemberlistinfo.length;j++){
                            vipArray.push(item);
                        }
                        $that.setData({ levelTable: vipArray })
                    }
                    $that.getMember();
                }
            }
        });
    },

    bindChange: function (e) {
      
        var that = this;
        that.setData({ currentTab: e.detail.current});
     
    },

    swichNav: function (e) {
       
        var that = this;
        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            });
        }
       
    },
    //用户和盟主的两种状态
    onChangeShowState: function (e) {
        var that = this;
        if (that.data.showView) {
          
            that.setData({
                showView: false,
                triangleStyle: false
            })
        } else {
            that.setData({
                showView: true,
                triangleStyle: true
            })
        }
        if (e.currentTarget.dataset.usertype==1){
            that.getLevel();
            that.setData({
                userPrivilege: that.data.buttonText+"特权",
                showCorrectImg:1,
                sendUserType: 1
                
            })
            
        } else if (e.currentTarget.dataset.usertype == 0){
            that.getLevel();
            that.setData({
                userPrivilege: "用户特权",
                showCorrectImg: 0,
                sendUserType:0
            })     
        }

    },


    skipToPay:function () {
        var $that = this;
        if($that.data.type==0){
            wx.showModal({
                title: '提示',
                content: '普通用户先回到个人中心申请贵族',
                success: function (res) {
                    if (res.confirm) {
                      
                        wx.switchTab({
                            url: '../my/index',
                        })

                    } else if (res.cancel) {
                      
                    }
                }
            })
        }else{
           
            wx.navigateTo({
                url: "../upgradeWithdraw/index"
            })
        }
        
       
       
    },
    upGrade:function(){
        var $that = this;
        if ($that.data.type == 0) {
            wx.showModal({
                title: '提示',
                content: '普通用户先回到个人中心申请贵族',
                success: function (res) {
                    if (res.confirm) {
                    
                        wx.switchTab({
                            url: '../my/index',
                            success: function (e) {
                                var page = getCurrentPages().pop();
                                if (page == undefined || page == null) return;
                                page.onLoad();
                            }
                        })

                    } else if (res.cancel) {
                      
                    }
                }
            })
        } else {
            
            
            if (parseFloat($that.data.credit1) -$that.data.lackUpNextCredit >= 0) {
                wx.showModal({
                    title: '提示',
                    content: '确认花掉' + $that.data.upNextGradeCredit+'积分升级吗？',
                    success: function (res) {
                        if (res.confirm) {
                         
                            app.util.request({
                                'url': 'entry/wxapp/LeveUp',
                                data: {
                                    "m": 'nets_haojk',
                                    "level": $that.data.newLevel,
                                },
                                success: function (res) {
                                    //成功后返回my页面
                                 
                                    wx.switchTab({
                                        url: "../my/index",
                                        success: function (e) {
                                            var page = getCurrentPages().pop();
                                            if (page == undefined || page == null) return;
                                            page.onLoad();
                                        }
                                    })
                                }
                            })
                        } else if (res.cancel) {
                            
                        }
                    }
                })
           } else {
                wx.navigateTo({
                   url: '../upgradeWithdraw/index',
                })
           }
        }
       
    },
    getMember: function () {
      var that = this;
      app.getUserInfo(function (userInfo) {
      
        //更新数据
        app.util.getUserInfo(function (response) {
          wx.showToast({
            title: '登陆成功'
          });
          response.memberInfo.nickname = userInfo.nickName;
          response.memberInfo.avatar = userInfo.avatarUrl;
          response.memberInfo.city = userInfo.city;
          response.memberInfo.country = userInfo.country;
          response.memberInfo.province = userInfo.province;
          response.memberInfo.gender = userInfo.gender;
          that.addMember(response.memberInfo);
        });
      });
      // 页面渲染完成
    },
    addMember: function (userInfo) {
      var $this = this;
      app.util.request({
        url: 'entry/wxapp/AddMember',
        data: {
          "m": 'nets_haojk',
          "from_uid": this.data.from_uid,
          "nickname": userInfo.nickname,
          "avatar": userInfo.avatar,
          "city": userInfo.city,
          "country": userInfo.country,
          "province": userInfo.province,
          "gender": userInfo.gender,
        },
        method: 'post',
        success: function (response) {
          //首页存储会员对象到缓存，其他页面直接读取缓存即可
          wx.setStorageSync("memberinfo",response.data.data);
          $this.initpage();
        }
      });
    },
    initpage:function(){
       
        var that = this
        var memberlistinfoObj = that.data.memberlistinfo;
        //取会员对象的缓存
       
        wx.getStorage({
            key: 'memberinfo',
            success: function (res) {
                that.setData(
                    {
                        //测试时关闭   真实时放开
                        currentTab: res.data.level,
                        type: res.data.type,
                        credit1: res.data.credit1,
                        memberinfo:res.data,
                    }
                )
              
                wx.setStorage(
                    {
                        key: "credit1",
                        data: res.data.credit1
                    }
                )
                if (res.data.type == 1) {
                    that.setData(
                        {
                            //测试时关闭   真实时放开
                            level: res.data.level,
                        }
                    )
                    //赋值和存缓存升级到的等级
                    var newMemberlistinfo = wx.getStorageSync('newMemberlistinfo');
                   

                    for (var i = 0; i < newMemberlistinfo.length;i++){
                    
                        if (newMemberlistinfo[i].name == res.data.level && i < newMemberlistinfo.length-1){
                           
                            that.setData({
                                newLevel: parseInt(newMemberlistinfo[i+1].name)

                            })
                           
                            wx.setStorage(
                                {
                                    key: "newLevel",
                                    data: parseInt(newMemberlistinfo[i + 1].name)
                                }
                            )

                        }
                    }
                   

                    wx.setStorage(
                         {
                            key: "level",
                            data: res.data.level
                        }
                    )
                    //显示盟主缺少的积分
                    wx.getStorage({
                        key: 'newMemberlistinfo',
                        success: function (res) {
                            console.log(res.data);
                          
                            var newMemberlistinfo = res.data;
                            for (var i = 0; i < newMemberlistinfo.length;i++){
                                if (newMemberlistinfo[i].name==that.data.level){
                                   
                                    //是否存在上一级
                                    if (newMemberlistinfo[i+1]){
                                        

                                        that.setData({ lackUpNextCredit: newMemberlistinfo[i+1].recharge_get - newMemberlistinfo[i].recharge_get - that.data.credit1,
                                            //升级需要花费的积分
                                            upNextGradeCredit: newMemberlistinfo[i + 1].recharge_get - newMemberlistinfo[i].recharge_get,
                                            //下一个等级的名称
                                            nextidentityname: newMemberlistinfo[i + 1].identityname,
                                            nextgradename: newMemberlistinfo[i + 1].gradename

                                        })
                                        wx.setStorage(
                                            {
                                                key: "lackUpNextCredit",
                                                data: newMemberlistinfo[i + 1].recharge_get - newMemberlistinfo[i].recharge_get - that.data.credit1,
                                                nextidentityname: newMemberlistinfo[i + 1].identityname,
                                                nextgradename: newMemberlistinfo[i + 1].gradename
                                            }
                                        )
                                    }
                                  
                                }
                            }

                        }
                    })
                    
                } else {

                }
            }
        });
    }
})